package com.anjho.pojo;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name = "user")
@NamedQueries({
		@NamedQuery(name = "User.getUserList", query = "SELECT u FROM User u"),
		@NamedQuery(name = "User.findByLogin", query = "SELECT u FROM User u WHERE u.login = :login"),
		@NamedQuery(name = "User.findByActive", query = "SELECT u FROM User u WHERE u.active = :active"),
		@NamedQuery(name = "User.findProfile", query = "SELECT u.profile FROM User u WHERE u.login = :login"),
		@NamedQuery(name = "User.findByLoginAndPassword", query = "SELECT u FROM User u WHERE u.login = :login and u.password = :password") })
public class User implements Serializable {

	private static final long serialVersionUID = 8015083950334827537L;
	
	public User() {
		this.setPerson(new Person());
	}

	public User(String login, String password) {
		this.login = login;
		this.password = password;
	}

	public User(String login, String password, Boolean active) {
		this.login = login;
		this.password = password;
		this.active = active;
	}

	@Id
	@OneToMany(mappedBy="user", cascade={CascadeType.PERSIST})
	@Column(name = "login", length = 32)
	private String login;

	@Column(name = "password", nullable = false, length = 32)
	private String password;

	@Column(name = "active", nullable = false)
	private Boolean active;

	@ManyToOne
	@JoinColumn(name = "procode", nullable = false)
	private Profile profile;

	@OneToOne(cascade = {CascadeType.PERSIST})
	@JoinColumn(name = "document", nullable = false, referencedColumnName = "document")
	private Person person;

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Boolean getActive() {
		return active;
	}

	public void setActive(Boolean active) {
		this.active = active;
	}

	public Profile getProfile() {
		return this.profile;
	}

	public void setProfile(Profile profile) {
		this.profile = profile;
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}
	
	public String getFormattedDocument() {
		return this.person.getDocument().substring(0, 3) + "."
				+ this.person.getDocument().substring(3, 6) + "."
				+ this.person.getDocument().substring(6, 9) + "-"
				+ this.person.getDocument().substring(9, 11);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((login == null) ? 0 : login.hashCode());
		result = prime * result
				+ ((password == null) ? 0 : password.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		User other = (User) obj;
		if (login == null) {
			if (other.login != null)
				return false;
		} else if (!login.equals(other.login))
			return false;
		if (password == null) {
			if (other.password != null)
				return false;
		} else if (!password.equals(other.password))
			return false;
		return true;
	}

}
